package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.GameUtil;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;

/**
 * ${陈虎}
 * 8:13
 * wp_maplestory_v1.0
 * ${"Do what you say,say what you do"}
 */

public class Mob extends AbstractMapleStoryObject{
    public boolean jump;
    public static Image[] imgs = new Image[100];
    static {
        for (int i = 0; i < 4; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_stand" +i);
        }
        for (int i = 4; i < 9; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_walk" +(i - 4));
        }
        for (int i = 9; i < 21; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_die" +(i - 9));
        }

        for (int i = 21; i < 25; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_stand" +(i -21));
        }
        for (int i = 25; i < 30; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_walk" +(i - 25));
        }
        for (int i = 30; i < 42; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_die" +(i - 30));
        }

    }
    public  int MAX_HP;
    public Mob(){
        this.width = imgs[0].getWidth(null);
        this.height = imgs[0].getHeight(null);
        this.dir = Direction.LEFT;
        this.action = Action.WALK;
        this.live = true;
        this.MAX_HP = 20000;
        this.HP = MAX_HP;
    }
    public Mob(MapleStoryClient msc,int x, int y,int speed){
        this();
        this.msc = msc;
        this.y = y;
        this.x = x;
        this.speed = speed;
    }
    private int count;
    private int step;
    private int dieStep;
    @Override
    public void draw(Graphics g) {
        if(dieStep >=11){
            this.live = false;
            dieStep = 0;
        }
        if(!live){
            msc.mobs.remove(this);
            return;
        }
        switch (dir){
            case LEFT:
                switch (action){
                    case STAND:
                        if(count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 4],x,y,null);
                        }else{
                            g.drawImage(imgs[step % 4],x,y,null);
                        }
                        break;

                    case WALK:
                        if(count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 5 + 4],x,y,null);
                        }else{
                            g.drawImage(imgs[step % 5 + 4],x,y,null);
                        }
                        break;
                    case DIE:
                        if(count++ % 2 == 0){
                            g.drawImage(imgs[dieStep++ % 12 + 9],x,y,null);
                        }else{
                            g.drawImage(imgs[dieStep % 12 + 9],x,y,null);
                        }
                        break;
                    case JUMP:
                        g.drawImage(imgs[8],x,y,null );
                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action){
                    case STAND:
                        if(count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 4 +21],x,y,null);
                        }else{
                            g.drawImage(imgs[step % 4 +21],x,y,null);
                        }
                        break;



                    case WALK:
                        if(count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 5 + 25],x,y,null);
                        }else{
                            g.drawImage(imgs[step % 5 + 25],x,y,null);
                        }
                        break;
                    case DIE:
                        if(count++ % 2 == 0){
                            g.drawImage(imgs[dieStep++ % 12 + 30],x,y,null);
                        }else{
                            g.drawImage(imgs[dieStep % 12 + 30],x,y,null);
                        }
                        break;
                    case JUMP:
                        g.drawImage(imgs[8],x,y,null );
                        break;
                    default:
                        break;
                }
                break;


            default:
                break;
        }
        if (HP < MAX_HP){
            bloodBar.draw(g);
        }

        move();

    }

    @Override
    public void move() {
        if(jump){
            jump(msc.grounds);
        }
        else{
            jumpDown(msc.grounds);
        }

        switch (dir){
            case LEFT:
                if (this.x <= 0 ){
                    this.dir = Direction.RIGHT;
                    this.action = Action.WALK;
                    x += speed;
                }
                x -= speed;
                break;
            case RIGHT:
                if (this.x >= Constant.GAME_WIDTH - this.width){


                    this.dir = Direction.LEFT;
                    this.action = Action.WALK;

                    x -= speed;
                }
                x += speed;
            default:
                break;
        }




        outOfBound();
    }


    private double v0 = Constant.INIT_JUMP_SPEED;
    private double vt;
    private double g = 9.8;
    private double delta_height;
    private double t = 0.5;

    /**
     * 跳跃的方法
     */

    public void jump(java.util.List<Ground> grounds){
        if (!drop){
            //向上跳(竖直上抛)
            vt = v0 - g * t;
            v0 = vt;
            y -= delta_height;
            //什么时候往下掉
            if (vt <= 0){
                vt = 0;
                drop = true;
            }

        }
        else{
            jumpDown(grounds);
        }
    }



    private boolean drop = true;
    public void jumpDown(List<Ground> grounds){
        //掉的过程(自由落体)
        vt = v0 + g * t;
        delta_height = v0 * t;
        v0 = vt;
        y += delta_height;


        for (int i = 0; i < grounds.size(); i++) {
            Ground ground = grounds.get(i);
            if (this.getRectangle().intersects(ground.getRectangle()) && this.y <= ground.y - this.height +40){
                //跳
                if (this.y >= ground.y - this.height){
                    this.y = ground.y - this.height;
                    drop = false;
                    jump = false;
                    v0 = Constant.INIT_JUMP_SPEED;
                    vt = 0.0;
                    break;

                }
            }else{
                drop = true;
            }
        }
    }




    public BloodBar bloodBar = new BloodBar();
    class BloodBar extends AbstractMapleStoryObject{
        private Image img;
        public BloodBar(){
            this.img = ImageUtil.getImage("blood");
            this.width = img.getWidth(null);
        }
        @Override
        public void draw(Graphics g) {
            /*Color c = g.getColor();

            g.drawRect(Mob.this.x,Mob.this.y - 15,Mob.this.width,10);
            g.setColor(Color.GREEN);
            g.fillRect(Mob.this.x,Mob.this.y - 15 + 1,(int)(Mob.this.width * Mob.this.HP / Mob.this.MAX_HP),10 - 1);
            g.setColor(c);*/
            int n = Mob.this.width / this.width * Mob.this.HP / Mob.this.MAX_HP;
            for (int i = 0; i < n; i++) {
                g.drawImage(img,Mob.this.x + (i * this.width + 1),Mob.this.y - 15,null);
            }


            /* g.drawImage(Mob.this.imgs[18],Mob.this.x,Mob.this.y - 15,null);*/
        }
    }
    /**
     * 判断出界
     */
    private void outOfBound(){
        if(this.x <= 0 ){
            this.x = 0;
        }
        if(this.x >= Constant.GAME_WIDTH - this.width){
            this.x = Constant.GAME_WIDTH - this.width;
        }
        if (this.x >= 540 - this.width && this.y >= 660 - this.width +40){
            this.x = 540 - this.width;

        }
        if (this.x >= 450 - this.width && this.y >= 720 - this.width +40){
            this.x = 450 - this.width;

        }
        if (this.x >= 450 - this.width && this.y >= 720 - this.height + 40){
            jump = true;
        }
        if (this.x >= 540 - this.width && this.y >= 660 - this.height + 40){
            jump = true;
        }
        hit(msc.hero);

    }
    private boolean hit(Hero hero){
        if(this.live && this.getRectangle().intersects(hero.getRectangle()) && hero.action != Action.DIE){



            // 创建出伤害值
//            Power power = new Power(msc,hero.x,hero.y);
//            msc.powers.add(power);
            hero.HP -= 20;

            if(hero.HP <= 0){
                hero.action = Action.DIE;

            }
            //mob.live = false;
            return true;
        }
        return false;
    }
}
